package com.enjoyor.soa.traffic.server.efoms.dubbo.listener;

import com.enjoyor.soa.traffic.api.dto.MessageInfoDto;
import com.enjoyor.soa.traffic.api.dto.collectms.DeviceStatusDto;
import com.enjoyor.soa.traffic.api.service.dubbo.collectms.IDubboCollectmsCallbackListener;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceDetailInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.server.efoms.common.Global;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboCollectCallbackServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.manage.ManageDevWranHandleService;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.*;

@Async
@Order(1)
@Component
public class DubboCollectCallBackListener implements CommandLineRunner,Serializable {
    String listenerId = "192.168.56.8c";
    
    @Autowired
    private DubboCollectCallbackServiceInvoke collectCallbackInvoke;
    
    @Autowired
    private ManageDevWranHandleService handleService;
    
    @Override
    public void run(String... args) throws Exception {
        startCallBack();
    }

    private void startCallBack() throws InterruptedException {
        addListener(listenerId);
        while (true) {
            try {
                if (collectCallbackInvoke != null) {
                	addListener(listenerId);
                }
            } catch (Exception e) {
                StackTraceElement StackTrace = Thread.currentThread().getStackTrace()[1];
                //LogInfoHelper.writeErrorLogInfo("设备运维回调", StackTrace, e);
            } finally {
                Thread.sleep(10000);
            }
        }
    }

    IDubboCollectmsCallbackListener listener;
    private IDubboCollectmsCallbackListener getListener() {
        if (listener == null) {
            listener = new IDubboCollectmsCallbackListener() {
                @Override
                @SuppressWarnings("unchecked")
                public void changed(ResultPojo msg) {
                    handler.handle(msg); 
                }
            };
        }
        return listener;
    }

    private void addListener(String name) {
        try {
        	collectCallbackInvoke.addCollectDevListener(name, getListener());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    BasicMsgHandler handler = new BasicMsgHandler<ResultPojo>() {
        /**
         * 获取回调，获取卡口设备状态变化数据
         * @param result
         * @return
         */
		@Override
        @SuppressWarnings("unchecked")
        protected boolean process(ResultPojo result) {
        		try {
        			System.out.println("collect=================="+TimeHelper.dateToLongString());
        			
        			List<DeviceStatusDto> collectStatusList = (List<DeviceStatusDto>)result.getResultList();
    	        	for(DeviceStatusDto collectStatusDto : collectStatusList) {
    	        		String typeCode = "";
    	        		String typeName = "";
    	        		if("01".equals(collectStatusDto.getType())) {
    	        			typeCode = EnumDicPublic.WAVELET.getKey();
    	            		typeName = EnumDicPublic.WAVELET.getValue();
    	            		//wranTacticsList = wranTacticsListWave;
    	        		}else if("02".equals(collectStatusDto.getType())) {
    	        			typeCode = EnumDicPublic.MAGNETISM.getKey();
    	            		typeName = EnumDicPublic.MAGNETISM.getValue();
    	            		//wranTacticsList = wranTacticsListMagn;
    	        		}else if("03".equals(collectStatusDto.getType())) {
    	        			typeCode = EnumDicPublic.WINDING.getKey();
    	            		typeName = EnumDicPublic.WINDING.getValue();
    	            		//wranTacticsList = wranTacticsListWind;
    	        		}else if("04".equals(collectStatusDto.getType())) {
    	        			typeCode = EnumDicPublic.VIDEOCHECK.getKey();
    	            		typeName = EnumDicPublic.VIDEOCHECK.getValue();
    	            		//wranTacticsList = wranTacticsListWind;
    	        		}else{
    	        			continue;
    	        		}
    	        		String devId = collectStatusDto.getDevId();
    	        		String key = typeCode + "-" + devId;
    	        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
    	        		String devName = "";
    	        		if(null != deviceDetailInfo) {
    	        			devName = deviceDetailInfo.getDevName();
    	        		}else {
    	        			continue;
    	        		}
    	        		//根据策略生成数据报警数据
    	        		String collectTime = collectStatusDto.getCollectTime();
    	        		String receiveTime = collectStatusDto.getReceiveTime();
    	        		String writeDbTime = collectStatusDto.getWriteDbTime();
    	        		//根据策略生成数据报警
    	        		handleService.devDataWranHandle(typeCode, typeName, devId, devName, collectTime, 
    	        				receiveTime, writeDbTime,null, "订阅采集状态");
    	        	}
                } catch (Exception e) {
                    StackTraceElement StackTrace = Thread.currentThread().getStackTrace()[1];
                    List listParam = Arrays.asList(result);
                    //LogInfoHelper.writeErrorLogInfo("处理缉查布控回调", StackTrace, listParam, e, ResultPojo.class);
                }
            return true;
        }
    };

}
